package com.ms.service.impl;

import com.ms.dao.VolunteerEnrollMapper;
import com.ms.entity.VolunteerEnroll;
import com.ms.service.inter.VolunteerActivityService;
import com.ms.service.inter.VolunteerEnrollService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 志愿者活动报名服务实现类
 */
@Service
public class VolunteerEnrollServiceImpl implements VolunteerEnrollService {

    @Autowired
    private VolunteerEnrollMapper volunteerEnrollMapper;
    
    @Autowired
    private VolunteerActivityService volunteerActivityService;

    /**
     * 添加报名记录
     * @param volunteerEnroll 报名信息
     * @return 是否成功
     */
    @Override
    public boolean addVolunteerEnroll(VolunteerEnroll volunteerEnroll) {
        int result = volunteerEnrollMapper.insert(volunteerEnroll);
        return result > 0;
    }

    /**
     * 根据ID删除报名记录
     * @param enrollId 报名记录ID
     * @return 是否成功
     */
    @Override
    public boolean deleteVolunteerEnrollById(Long enrollId) {
        int result = volunteerEnrollMapper.deleteById(enrollId);
        return result > 0;
    }

    /**
     * 更新报名记录
     * @param volunteerEnroll 报名信息
     * @return 是否成功
     */
    @Override
    public boolean updateVolunteerEnroll(VolunteerEnroll volunteerEnroll) {
        int result = volunteerEnrollMapper.updateById(volunteerEnroll);
        return result > 0;
    }

    /**
     * 根据ID获取报名记录
     * @param enrollId 报名记录ID
     * @return 报名记录
     */
    @Override
    public VolunteerEnroll getVolunteerEnrollById(Long enrollId) {
        return volunteerEnrollMapper.selectById(enrollId);
    }

    /**
     * 获取所有报名记录
     * @return 报名记录列表
     */
    @Override
    public List<VolunteerEnroll> getAllVolunteerEnrolls() {
        return volunteerEnrollMapper.selectList(null);
    }

    /**
     * 根据志愿者ID获取报名记录
     * @param volunteerId 志愿者ID
     * @return 报名记录列表
     */
    @Override
    public List<VolunteerEnroll> getVolunteerEnrollsByVolunteerId(Long volunteerId) {
        return volunteerEnrollMapper.selectByVolunteerId(volunteerId);
    }

    /**
     * 根据活动ID获取报名记录
     * @param activityId 活动ID
     * @return 报名记录列表
     */
    @Override
    public List<VolunteerEnroll> getVolunteerEnrollsByActivityId(Long activityId) {
        return volunteerEnrollMapper.selectByActivityId(activityId);
    }

    /**
     * 通过报名申请
     * @param enrollId 报名记录ID
     * @return 是否成功
     */
    @Override
    public boolean approveEnroll(Long enrollId) {
        // 获取报名记录
        VolunteerEnroll enroll = volunteerEnrollMapper.selectById(enrollId);
        if (enroll == null) {
            throw new RuntimeException("报名记录不存在");
        }

        // 获取活动信息
        Long activityId = enroll.getActivityId();
        com.ms.entity.VolunteerActivity activity = volunteerActivityService.findById(activityId);
        if (activity == null) {
            throw new RuntimeException("活动信息不存在");
        }

        // 检查活动状态是否为招募中
        if (activity.getStatus() != 1) {
            throw new RuntimeException("活动不在招募中状态，无法通过报名");
        }

        // 检查招募人数是否已满
        if (activity.getRecruitedNum() >= activity.getRecruitNum()) {
            throw new RuntimeException("活动招募人数已满，无法继续招募");
        }

        // 更新报名记录状态为已通过
        enroll.setEnrollStatus(1); // 1表示已通过
        enroll.setAuditTime(LocalDateTime.now());
        int enrollResult = volunteerEnrollMapper.updateById(enroll);

        // 更新活动已招募人数
        activity.setRecruitedNum(activity.getRecruitedNum() + 1);
        volunteerActivityService.update(activity);

        return enrollResult > 0;
    }

    /**
     * 取消通过的报名申请
     * @param enrollId 报名记录ID
     * @return 是否成功
     */
    @Override
    public boolean cancelEnroll(Long enrollId) {
        // 获取报名记录
        VolunteerEnroll enroll = volunteerEnrollMapper.selectById(enrollId);
        if (enroll == null) {
            throw new RuntimeException("报名记录不存在");
        }

        // 检查报名状态是否为已通过
        if (enroll.getEnrollStatus() != 1) {
            throw new RuntimeException("报名未通过审核，无法取消");
        }

        // 获取活动信息
        Long activityId = enroll.getActivityId();
        com.ms.entity.VolunteerActivity activity = volunteerActivityService.findById(activityId);
        if (activity == null) {
            throw new RuntimeException("活动信息不存在");
        }

        // 更新报名记录状态为待审核
        enroll.setEnrollStatus(0); // 0表示待审核
        enroll.setAuditTime(LocalDateTime.now());
        int enrollResult = volunteerEnrollMapper.updateById(enroll);

        // 更新活动已招募人数（减一）
        if (activity.getRecruitedNum() > 0) {
            activity.setRecruitedNum(activity.getRecruitedNum() - 1);
            volunteerActivityService.update(activity);
        }

        return enrollResult > 0;
    }
    
    /**
     * 拒绝报名申请
     * @param enrollId 报名记录ID
     * @return 是否成功
     */
    @Override
    public boolean rejectEnroll(Long enrollId) {
        // 获取报名记录
        VolunteerEnroll enroll = volunteerEnrollMapper.selectById(enrollId);
        if (enroll == null) {
            throw new RuntimeException("报名记录不存在");
        }

        // 检查报名状态是否为待审核
        if (enroll.getEnrollStatus() != 0) {
            throw new RuntimeException("只能拒绝待审核状态的报名申请");
        }

        // 更新报名记录状态为已拒绝
        enroll.setEnrollStatus(2); // 2表示已拒绝
        enroll.setAuditTime(LocalDateTime.now());
        int enrollResult = volunteerEnrollMapper.updateById(enroll);

        return enrollResult > 0;
    }
    
    /**
     * 根据志愿者ID和活动ID获取特定报名记录
     * @param volunteerId 志愿者ID
     * @param activityId 活动ID
     * @return 报名记录
     */
    @Override
    public VolunteerEnroll getVolunteerEnrollByVolunteerIdAndActivityId(Long volunteerId, Long activityId) {
        return volunteerEnrollMapper.selectByVolunteerIdAndActivityId(volunteerId, activityId);
    }
}